<script setup lang="ts">
import {useRoute, useRouter} from "vue-router";
import {compile, ref} from "vue";
import requestApi from "../plugins/requestApi.ts";
import {showFailToast, showSuccessToast} from "vant";

const route = useRoute();
const router = useRouter();
const editUser = ref({
  id: route.query.id,
  editKey: route.query.editKey,
  currentValue: route.query.currentValue,
  editName: route.query.editName,
})
console.log(editUser.value);
const onSubmit = (values) => {
  requestApi.post('/user/update', {
    "id": `${editUser.value.id}`,
    [editUser.value.editKey]: values[editUser.value.editKey],
  }).then(res => {
    console.log(res);
    if (res) {
      showSuccessToast("修改成功");
      router.back()
    } else {
      showFailToast("修改失败");
    }
  }).catch(err => {
    showFailToast("修改失败");
  })
}
</script>

<template>
  <van-form @submit="onSubmit">
    <van-field
        v-model="editUser.currentValue"
        :name="editUser.editKey"
        :label="editUser.editName"
        :placeholder="`请输入${editUser.editName}`"
    />
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<style scoped>

</style>